class Tool{
  constructor(){

  }
  /**
  * 判断某个元素是否为undefined
  * @param  {undefined}  value 
  * @return {Boolean}       
  */
  isUndefined(value) {
    return typeof value === 'undefined'
  }

  /**
   * 编码URI
   * @param  {String} value 
   * @param  {String} pctEncodeSpaces 
   * @return {String} 
   */
  encodeUriQuery(value, pctEncodeSpaces) {
    return encodeURIComponent(value)
      .replace(/%40/gi, '@')
      .replace(/%3A/gi, ':')
      .replace(/%24/g, '$')
      .replace(/%2C/gi, ',')
      .replace(/%3B/gi, ';')
      .replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'))
  }


  /**
   * 对象序列化
   * @param  {Object} obj 
   * @return {String} 
   */
  paramSerializer(obj) {
    if (!obj) return ''
    let that = this
    let parts = []
    for (let key in obj) {
      const value = obj[key]
      if (value === null || that.isUndefined(value)) return
      if (that.isArray(value)) {
        value.forEach(function (v) {
          parts.push(that.encodeUriQuery(key) + '=' + that.encodeUriQuery(that.serializeValue(v)))
        })
      } else {
        parts.push(that.encodeUriQuery(key) + '=' + that.encodeUriQuery(that.serializeValue(value)))
      }
    }
    return parts.join('&')
  }

  /**
   * 拼接URL
   * @param  {String} obj 
   * @param  {Object} obj 
   * @return {String} 
   */
  buildUrl(url, obj) {
    const serializedParams = this.paramSerializer(obj)
    if (serializedParams.length > 0) {
      url += ((url.indexOf('?') == -1) ? '?' : '&') + serializedParams
    }
    return url
  }
}

export default Tool